import os
import pygetwindow as gw
import win32gui
import time
import pyautogui

from my_package.xqol_utils import get_window_size, find_btn_location_once, get_windows_with_exact_title

# 定义相关图片位置
kuai_su_deng_lu_img_path = "../images/auto_login/kuai_su_deng_lu.png"
dian_nao_yi_qu_path = "../images/auto_login/dian_nao_yi_qu.png"
confirm_btn_img_path = '../images/auto_login/que_ren.png'

def do_open_game_window(desktop_path, account_list):
    my_game_window = []
    for ob in account_list:
        title = ob["window_title"]
        # 先判断有没有已经启动了
        #windows = gw.getWindowsWithTitle(ob["window_title"])
        windows = get_windows_with_exact_title(ob["window_title"])
        if len(windows) == 0:
            print("程序还没启动，准备打开")
            # 拼接 路径
            shortcut_path = os.path.join(desktop_path, ob["link_name"])
            print(shortcut_path)
            os.startfile(shortcut_path)
            # 等待启动
            time.sleep(3)
            # 重新获取窗口
            #windows = gw.getWindowsWithTitle(ob["window_title"])
            windows = get_windows_with_exact_title(ob["window_title"])
        if len(windows) == 0:
            print(title, "启动失败！！！")
            continue
        window = windows[0]
        print(window.title, "已启动")
        time.sleep(0.5)
        # 激活窗口
        try:
            # 将窗口激活并置顶
            window.activate()
        except Exception as e:
            print("窗口操作失败：", e)
        my_game_window.append(window)
        # 获取窗口的外框矩形（包括标题栏和边框）
        rect = win32gui.GetWindowRect(window._hWnd)
        window_x, window_y, right, bottom = rect
        # 当距离顶部为0的时候，认定已经移动好了
        if window_y != 0:
            pyautogui.moveTo(window_x + 15, window_y + 10)
            time.sleep(2)
            pyautogui.dragTo(ob["x"], ob["y"] + 1, 0.5)
        print(window.title, "加载完毕")
    return my_game_window

def do_login_game(account_list):
    for ob in account_list:
        # 先判断有没有已经启动了
        #windows = gw.getWindowsWithTitle(ob["window_title"])
        windows = get_windows_with_exact_title(ob["window_title"])
        if len(windows) > 0:
            window = windows[0]
            time.sleep(0.5)
            # 激活窗口
            try:
                # 将窗口激活并置顶
                window.activate()
            except Exception as e:
                print("窗口操作失败：", e)
            res = get_window_size(window._hWnd)
            width_main, height_main, window_x_main, window_y_main = res
            monitor = {"top": window_y_main, "left": window_x_main, "width": width_main, "height": height_main}
            # 去找[快速登录]按钮
            time.sleep(0.5)
            res_auto = find_btn_location_once(monitor, kuai_su_deng_lu_img_path)
            if res_auto:
                print(window.title, "找到 快速登录按钮", res_auto)
                x_auto, y_auto = res_auto
                pyautogui.moveTo(x_auto + window_x_main, y_auto + window_y_main)
                pyautogui.click()
            else:
                print("没有找到快速登录按钮")
            # 找[电脑一区]
            time.sleep(1)
            res_pc_1 = find_btn_location_once(monitor, dian_nao_yi_qu_path)
            if res_pc_1:
                print(window.title, "找到[电脑一区]", res_pc_1)
                x_pc_1, y_pc_1 = res_pc_1
                pyautogui.moveTo(x_pc_1 + window_x_main, y_pc_1 + window_y_main)
                pyautogui.click()
            else:
                print(window.title, "没有找到[电脑一区]")
                continue
            time.sleep(1)
            # 找[确认按钮]
            res_confirm = find_btn_location_once(monitor, confirm_btn_img_path)
            if res_confirm:
                x_confirm, y_confirm = res_confirm
                print(window.title, " 找到确认按钮", x_confirm, y_confirm)
                pyautogui.moveTo(x_confirm + window_x_main + 5, y_confirm + window_y_main + 5)
                pyautogui.click()
                pyautogui.click()
                # 激活[快速寻路]插件
                time.sleep(1)
                click_path_finding(ob)

def click_path_finding(ob):
    # 找插件
    plugin_x = ob["x"] + 35
    plugin_y = ob["y"] + 38
    # 鼠标移动到插件选择上
    pyautogui.moveTo(plugin_x, plugin_y)
    time.sleep(1)
    # 点击
    pyautogui.click()
    # 点击[快速寻路]
    plugin_y_fast = ob["y"] + 172
    pyautogui.moveTo(plugin_x, plugin_y_fast)
    # 选择[快速寻路]
    pyautogui.click()